const axios = require('axios');

// 测试配置
const BASE_URL = 'http://localhost:3000';
const TEST_TOKEN = 'your-test-token-here'; // 需要替换为实际的测试token

async function testExpensesAPI() {
  try {
    console.log('🚀 开始测试 Expenses API...\n');

    // 测试获取费用记录列表
    console.log('📋 测试获取费用记录列表...');
    const response = await axios.get(`${BASE_URL}/api/expenses`, {
      headers: {
        'Authorization': `Bearer ${TEST_TOKEN}`,
        'Content-Type': 'application/json'
      },
      params: {
        page: 1,
        limit: 5,
        vehicleId: 'your-vehicle-id' // 需要替换为实际的车辆ID
      }
    });

    if (response.status === 200) {
      console.log('✅ 获取费用记录列表成功');
      console.log('📊 响应数据结构:');
      
      const { expenses, pagination } = response.data.data;
      
      console.log(`📄 总记录数: ${pagination.total}`);
      console.log(`📄 当前页记录数: ${expenses.length}`);
      
      // 检查时间戳字段
      expenses.forEach((expense, index) => {
        console.log(`\n📝 记录 ${index + 1}:`);
        console.log(`   ID: ${expense._id}`);
        console.log(`   类型: ${expense.type}`);
        console.log(`   里程: ${expense.mileage}km`);
        console.log(`   创建时间戳: ${expense.createdAtTimestamp}`);
        console.log(`   更新时间戳: ${expense.updatedAtTimestamp}`);
        console.log(`   费用日期时间戳: ${expense.dateTimestamp}`);
        
        // 检查统计信息（从第二条开始）
        if (index > 0 && expense.stats) {
          console.log(`   📊 统计信息:`);
          console.log(`      行驶里程: ${expense.stats.distance}km`);
          console.log(`      百公里油耗: ${expense.stats.fuelConsumptionPer100km}L/100km`);
          console.log(`      百公里费用: ${expense.stats.costPer100km}元/100km`);
          console.log(`      消耗油量: ${expense.stats.fuelConsumption}L`);
          console.log(`      上次里程: ${expense.stats.previousMileage}km`);
          console.log(`      当前里程: ${expense.stats.currentMileage}km`);
        } else if (index === 0) {
          console.log(`   📊 统计信息: 第一条记录，无统计信息`);
        } else {
          console.log(`   📊 统计信息: 非加油记录，无统计信息`);
        }
      });
      
    } else {
      console.log('❌ 获取费用记录列表失败');
      console.log('状态码:', response.status);
      console.log('响应:', response.data);
    }

  } catch (error) {
    console.error('❌ 测试失败:', error.message);
    if (error.response) {
      console.error('响应状态:', error.response.status);
      console.error('响应数据:', error.response.data);
    }
  }
}

// 运行测试
if (require.main === module) {
  console.log('⚠️  注意: 请先确保:');
  console.log('   1. 服务器正在运行 (npm run dev)');
  console.log('   2. 更新 TEST_TOKEN 为有效的认证token');
  console.log('   3. 更新 vehicleId 为有效的车辆ID');
  console.log('');
  
  testExpensesAPI();
}

module.exports = { testExpensesAPI };
